﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Waf.Applications;
using System.Windows.Input;
using HighLandirect.Applications.Views;
using HighLandirect.Domain;
using System;
using System.Windows.Controls;
using System.Linq;

namespace HighLandirect.Applications.ViewModels
{
    public class CustomerListViewModel : ViewModel<ICustomerListView>
    {
        private readonly IEnumerable<Customer> customers;
        private readonly ObservableCollection<Customer> selectedCustomers;
        private Customer selectedCustomer;
        private ICommand addNewCommand;
        private ICommand removeCommand;
        private ICommand searchCommand;
        private ICommand addSendCustomerCommand;
        private ICommand addResceiveCustomerCommand;
        private ICommand printAtenaSealCommand;
        private ICommand printKokyakuDaichoCommand;
        private IEnumerable<Customer> customersView;

        private ICustomerListView customerListView;


        public CustomerListViewModel(ICustomerListView view, IEnumerable<Customer> customers)
            : base(view)
        {
            if (customers == null) { throw new ArgumentNullException("customers"); }

            this.customers = customers;
            this.HideDeletedData();
            this.selectedCustomers = new ObservableCollection<Customer>();

            this.customerListView = view;
        }

        public void ScrollIntoView()
        {
            var view = this.customerListView as ContentControl;
            var grid = ((Panel)view.Content).Children[1] as DataGrid;
            grid.ScrollIntoView(this.selectedCustomer);
        }


        public IEnumerable<Customer>Customers { get { return customers; } }

        public ObservableCollection<Customer> SelectedCustomers
        {
            get { return selectedCustomers; }
        }

        public Customer SelectedCustomer
        {
            get { return selectedCustomer; }
            set
            {
                if (selectedCustomer != value)
                {
                    selectedCustomer = value;
                    RaisePropertyChanged("SelectedCustomer");
                }
            }
        }

        public string SearchString { get; set; }

        public ICommand AddNewCommand
        {
            get { return addNewCommand; }
            set
            {
                if (addNewCommand != value)
                {
                    addNewCommand = value;
                    RaisePropertyChanged("AddNewCommand");
                }
            }
        }

        public ICommand RemoveCommand
        {
            get { return removeCommand; }
            set
            {
                if (removeCommand != value)
                {
                    removeCommand = value;
                    RaisePropertyChanged("RemoveCommand");
                }
            }
        }

        public ICommand SearchCommand
        {
            get { return searchCommand; }
            set
            {
                if (searchCommand != value)
                {
                    searchCommand = value;
                    RaisePropertyChanged("SearchCommand");
                }
            }
        }

        public ICommand AddSendCustomerCommand
        {
            get { return addSendCustomerCommand; }
            set
            {
                if (addSendCustomerCommand != value)
                {
                    addSendCustomerCommand = value;
                    RaisePropertyChanged("AddSendCustomerCommand");
                }
            }
        }

        public ICommand AddResceiveCustomerCommand
        {
            get { return addResceiveCustomerCommand; }
            set
            {
                if (addResceiveCustomerCommand != value)
                {
                    addResceiveCustomerCommand = value;
                    RaisePropertyChanged("AddResceiveCustomerCommand");
                }
            }
        }

        public ICommand PrintAtenaSealCommand
        {
            get { return printAtenaSealCommand; }
            set
            {
                if (printAtenaSealCommand != value)
                {
                    printAtenaSealCommand = value;
                    RaisePropertyChanged("PrintAtenaSealCommand");
                }
            }
        }

        public ICommand PrintKokyakuDaichoCommand
        {
            get { return printKokyakuDaichoCommand; }
            set
            {
                if (printKokyakuDaichoCommand != value)
                {
                    printKokyakuDaichoCommand = value;
                    RaisePropertyChanged("PrintKokyakuDaichoCommand");
                }
            }
        }

        public void ShowDeletedData()
        {
            this.customersView = this.customers.Where(x => x.Delete == true);
            this.RaisePropertyChanged("Customers");
        }

        public void HideDeletedData()
        {
            this.customersView = this.customers.Where(x => x.Delete != true);
            this.RaisePropertyChanged("Customers");
        }
    }
}
